SL:Require("GUILayout/TmpRecord", false)

local ui = {}

ui.type_idx = 1
ui.select_idx = 1

ui.last_pos_x = 0

ui.block_once = false

-- 100 / seconds
-- ui.udt_scroll_move_speed = 20

ui.split_pos_list = {-104.1612, -324.8817, -534.105, -743.457}


function ui.init(parent)
	local beforeScreenSize = GUI:getContentSize(parent)
	-- SL:Print("map_teleport_view width:", beforeScreenSize.width, " height:", beforeScreenSize.height)
	-- local pp = GUI:getParent(parent)
	-- SL:Print("map_teleport_view 11 name:" .. GUI:getName(pp))
	-- local pp2 = GUI:getParent(pp)
	-- SL:Print("map_teleport_view 22 name:" .. GUI:getName(pp2))
	-- local pp3 = GUI:getParent(pp2)
	-- SL:Print("map_teleport_view 33 name:" .. GUI:getName(pp3))
	-- local pp4 = GUI:getParent(pp3)
	-- SL:Print("map_teleport_view 44 name:" .. GUI:getName(pp4))
	ui.type_idx = GetKeyValue("SelType") or 1
	ui.select_idx = GetKeyValue("SelIdx") or 1
	SL:Print("map_teleport_view SelType:" .. ui.type_idx .. " SelIdx:" .. ui.select_idx)
	-- Create UI_MAP_TELEPORT
	local UI_MAP_TELEPORT = GUI:Layout_Create(parent, "UI_MAP_TELEPORT", beforeScreenSize.width / 2, beforeScreenSize.height / 2, 1136.00, 640.00, false)
	GUI:setChineseName(UI_MAP_TELEPORT, "UI_MAP_TELEPORT")
	GUI:setAnchorPoint(UI_MAP_TELEPORT, 0.5, 0.5)
	GUI:setTouchEnabled(UI_MAP_TELEPORT, false)
	GUI:setTag(UI_MAP_TELEPORT, -1)

	-- Create ImageViewBG
	local ImageViewBG = GUI:Image_Create(UI_MAP_TELEPORT, "ImageViewBG", 568.00, 320.00,
		"res/peng_cheng/view_enter_map/bg_enter_map.png")
	GUI:setAnchorPoint(ImageViewBG, 0.50, 0.50)
	GUI:setTouchEnabled(ImageViewBG, true)
	GUI:setSwallowTouches(ImageViewBG, true)
	GUI:setTag(ImageViewBG, -1)

	-- Create ListViewTitle
	local ListViewTitle = GUI:ListView_Create(UI_MAP_TELEPORT, "ListViewTitle", 568.00, 483.00, 658.00, 82.00, 2)
	GUI:ListView_setGravity(ListViewTitle, 3)
	GUI:setAnchorPoint(ListViewTitle, 0.50, 0.50)
	GUI:setTouchEnabled(ListViewTitle, true)
	GUI:setSwallowTouches(ListViewTitle, true)
	GUI:setTag(ListViewTitle, -1)

	-- Create ScrollView
	local UdtScrollView = GUI:Layout_Create(UI_MAP_TELEPORT, "UdtScrollView", 568.00, 312.00, 674.00, 248.00, true)
	GUI:setAnchorPoint(UdtScrollView, 0.50, 0.50)
	GUI:setTouchEnabled(UdtScrollView, true)
	GUI:setSwallowTouches(UdtScrollView, true)
	GUI:setTag(UdtScrollView, -1)

	local UdtInnerView = GUI:Widget_Create(UdtScrollView, "UdtInnerView", 0, 124, 1572, 248)
	UdtInnerView.left_limit_x = 674 - 1572
	-- local ScrollView = GUI:ScrollView_Create(UI_MAP_TELEPORT, "ScrollView", 568.00, 312.00, 674.00, 248.00, 2)
	GUI:setAnchorPoint(UdtInnerView, 0, 0.50)
	GUI:setTouchEnabled(UdtInnerView, false)
	GUI:setSwallowTouches(UdtInnerView, false)
	-- GUI:ScrollView_setInnerContainerSize(ScrollView, 1572, 248)
	-- GUI:ScrollView_setBackGroundColor(ScrollView, "#000000")
	GUI:setTag(UdtInnerView, -1)




	-- Create ImgMap1
	local ImgMap1 = GUI:Image_Create(UdtInnerView, "ImgMap1", 101, 124.00, "res/peng_cheng/view_enter_map/map_1_1.png")
	GUI:setAnchorPoint(ImgMap1, 0.50, 0.50)
	GUI:setScaleX(ImgMap1, 0.7)
	GUI:setScaleY(ImgMap1, 0.7)
	GUI:setTouchEnabled(ImgMap1, true)
	GUI:setTag(ImgMap1, -1)
	GUI:setSwallowTouches(ImgMap1, false)

	-- Create ImgMap2
	local ImgMap2 = GUI:Image_Create(UdtInnerView, "ImgMap2", 319, 124.00, "res/peng_cheng/view_enter_map/map_1_2.png")
	GUI:setAnchorPoint(ImgMap2, 0.50, 0.50)
	GUI:setScaleX(ImgMap2, 0.7)
	GUI:setScaleY(ImgMap2, 0.7)
	GUI:setTouchEnabled(ImgMap2, true)
	GUI:setTag(ImgMap2, -1)
	GUI:setSwallowTouches(ImgMap2, false)

	-- Create ImgMap3
	local ImgMap3 = GUI:Image_Create(UdtInnerView, "ImgMap3", 537, 124.00, "res/peng_cheng/view_enter_map/map_1_3.png")
	GUI:setAnchorPoint(ImgMap3, 0.50, 0.50)
	GUI:setScaleX(ImgMap3, 0.7)
	GUI:setScaleY(ImgMap3, 0.7)
	GUI:setTouchEnabled(ImgMap3, true)
	GUI:setTag(ImgMap3, -1)
	GUI:setSwallowTouches(ImgMap3, false)

	-- Create ImgMap4
	local ImgMap4 = GUI:Image_Create(UdtInnerView, "ImgMap4", 755, 124.00, "res/peng_cheng/view_enter_map/map_1_4.png")
	GUI:setAnchorPoint(ImgMap4, 0.50, 0.50)
	GUI:setScaleX(ImgMap4, 0.7)
	GUI:setScaleY(ImgMap4, 0.7)
	GUI:setTouchEnabled(ImgMap4, true)
	GUI:setTag(ImgMap4, -1)
	GUI:setSwallowTouches(ImgMap4, false)

	-- Create ImgMap5
	local ImgMap5 = GUI:Image_Create(UdtInnerView, "ImgMap5", 973, 124.00, "res/peng_cheng/view_enter_map/map_1_5.png")
	GUI:setAnchorPoint(ImgMap5, 0.50, 0.50)
	GUI:setScaleX(ImgMap5, 0.7)
	GUI:setScaleY(ImgMap5, 0.7)
	GUI:setTouchEnabled(ImgMap5, true)
	GUI:setTag(ImgMap5, -1)
	GUI:setSwallowTouches(ImgMap5, false)

    GUI:addOnTouchEvent(UdtScrollView, function(sender, type)
		ui.udtScrollViewTouchEvent(sender, type, UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5})
    end)


	-- ScrollView onScroll event
	-- GUI:ScrollView_addOnScrollEvent(ScrollView, function (param1, param2)
	-- 	-- SL:Print("ScrollEvent param1:", SL:JsonEncode(param1, false), " param2:", param2)
	-- 	local pos = GUI:ScrollView_getInnerContainerPosition(ScrollView)
	-- 	if 0 == ui.last_pos_x then
	-- 		ui.last_pos_x = pos.x
	-- 	end
	-- 	if ui.last_pos_x == pos.x then
	-- 		return
	-- 	end
	-- 	-- ui.split_pos_list = {-104.1612, -324.8817, -534.105, -743.457}
	-- 	if 0 < ui.last_pos_x - pos.x then
	-- 		--Ascending (from right to left)
	-- 		local dst_idx = 5
	-- 		for i = 1, #ui.split_pos_list do
	-- 			if ui.split_pos_list[i] < pos.x then
	-- 				dst_idx = i
	-- 				break
	-- 			end
	-- 		end
	-- 		SL:Print("ScrollEvent 1 pos.x:", pos.x, " dst_idx:", dst_idx, " ui.select_idx:", ui.select_idx, " param2:", param2)
	-- 		if dst_idx ~= ui.select_idx then
	-- 			ui.select_idx = 1
	-- 			GUI:setTouchEnabled(ScrollView, false)
	-- 			ui.delayCallback(ScrollView, 0.5, function ()
	-- 				GUI:setTouchEnabled(ScrollView, true)
	-- 			end)
	-- 			ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 		end
	-- 	else
	-- 		-- Descending (from left to right)
	-- 		local dst_idx = 1
	-- 		for i = #ui.split_pos_list, 1, -1 do
	-- 			if ui.split_pos_list[i] > pos.x then
	-- 				dst_idx = i + 1
	-- 				break
	-- 			end
	-- 		end
	-- 		SL:Print("ScrollEvent 2 pos.x:", pos.x, " dst_idx:", dst_idx, " ui.select_idx:", ui.select_idx, " param2:", param2)
	-- 		if dst_idx ~= ui.select_idx then
	-- 			ui.select_idx = dst_idx
	-- 			GUI:setTouchEnabled(ScrollView, false)
	-- 			ui.delayCallback(ScrollView, 0.5, function ()
	-- 				GUI:setTouchEnabled(ScrollView, true)
	-- 			end)
	-- 			ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 		end
	-- 	end

	-- 	-- local percent = (0 - pos.x) * 100 / 858
	-- 	-- -- SL:Print("Innerosition x:" .. pos.x .. " percent:" .. percent .. " param2:" .. param2)
	-- 	-- if 12.14 > percent then
	-- 	-- 	if 1 ~= ui.select_idx then
	-- 	-- 		ui.select_idx = 1
	-- 	-- 		ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 	-- 	end
	-- 	-- elseif 37.865 > percent then
	-- 	-- 	if 2 ~= ui.select_idx then
	-- 	-- 		ui.select_idx = 2
	-- 	-- 		ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 	-- 	end
	-- 	-- elseif 62.25 > percent then
	-- 	-- 	if 3 ~= ui.select_idx then
	-- 	-- 		ui.select_idx = 3
	-- 	-- 		ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 	-- 	end
	-- 	-- elseif 86.65 > percent then
	-- 	-- 	if 4 ~= ui.select_idx then
	-- 	-- 		ui.select_idx = 4
	-- 	-- 		ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 	-- 	end
	-- 	-- else
	-- 	-- 	if 5 ~= ui.select_idx then
	-- 	-- 		ui.select_idx = 5
	-- 	-- 		ui.resetItemPosWithAction({ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.5)
	-- 	-- 	end
	-- 	-- end
	-- 	-- if 10 == param2 then
	-- 	-- 	-- The touch has ended. Center the selected button here.
	-- 	-- 	ui.scrollToPercent(ScrollView, ui.select_idx, 0.3)
	-- 	-- end
	-- end)
	GUI:addOnClickEvent(ImgMap1, function ()
		-- ui.setBtnScaleEffect(ImgMap1)
		if ui.block_once then
			ui.block_once = false
			return
		end
		ui.select_idx = 1
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		-- ui.sendEnterMap(i.type_idx, ui.select_idx)
		ui.last_pos_x = 0
	end)
	GUI:addOnClickEvent(ImgMap2, function ()
		if ui.block_once then
			ui.block_once = false
			return
		end
		-- ui.setBtnScaleEffect(ImgMap2)
		ui.select_idx = 2
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		-- ui.sendEnterMap(i.type_idx, ui.select_idx)
		ui.last_pos_x = 0
	end)
	GUI:addOnClickEvent(ImgMap3, function ()
		if ui.block_once then
			ui.block_once = false
			return
		end
		-- ui.setBtnScaleEffect(ImgMap3)
		ui.select_idx = 3
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		-- ui.sendEnterMap(i.type_idx, ui.select_idx)
		ui.last_pos_x = 0
	end)
	GUI:addOnClickEvent(ImgMap4, function ()
		if ui.block_once then
			ui.block_once = false
			return
		end
		-- ui.setBtnScaleEffect(ImgMap4)
		ui.select_idx = 4
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		-- ui.sendEnterMap(i.type_idx, ui.select_idx)
		ui.last_pos_x = 0
	end)
	GUI:addOnClickEvent(ImgMap5, function ()
		if ui.block_once then
			ui.block_once = false
			return
		end
		-- ui.setBtnScaleEffect(ImgMap5)
		ui.select_idx = 5
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		-- ui.sendEnterMap(i.type_idx, ui.select_idx)
		ui.last_pos_x = 0
	end)
	ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.01)
	ui.last_pos_x = 0

	-- Create ImageView1
	local ImageView1 = GUI:Image_Create(ListViewTitle, "ImageView1", 78.5, 44.00,
		"res/peng_cheng/view_enter_map/title1.png")
	GUI:setAnchorPoint(ImageView1, 0.50, 0.50)
	GUI:setTouchEnabled(ImageView1, true)
	GUI:setTag(ImageView1, -1)
	GUI:setSwallowTouches(ImageView1, true)

	-- Create ImageView2
	local ImageView2 = GUI:Image_Create(ListViewTitle, "ImageView2", 235.5, 44.00,
		"res/peng_cheng/view_enter_map/title2.png")
	GUI:setAnchorPoint(ImageView2, 0.50, 0.50)
	GUI:setTouchEnabled(ImageView2, true)
	GUI:setTag(ImageView2, -1)
	GUI:setSwallowTouches(ImageView2, true)

	-- Create ImageView3
	local ImageView3 = GUI:Image_Create(ListViewTitle, "ImageView3", 392.5, 44.00,
		"res/peng_cheng/view_enter_map/title3.png")
	GUI:setAnchorPoint(ImageView3, 0.50, 0.50)
	GUI:setTouchEnabled(ImageView3, true)
	GUI:setTag(ImageView3, -1)
	GUI:setSwallowTouches(ImageView3, true)

	local type_btn_list = {ImageView1, ImageView2, ImageView3}
	local function typeBtnCallback(i)
		ui.type_idx = i
		SetKeyValue("SelType", ui.type_idx)
		ui.setTypeBtnScaleEffect(type_btn_list[i], nil, function (obj)
			ui.selectTypeShow(type_btn_list, i)
		end)
		GUI:Image_loadTexture(ImgMap1, "res/peng_cheng/view_enter_map/map_" .. i .. "_1.png")
		GUI:Image_loadTexture(ImgMap2, "res/peng_cheng/view_enter_map/map_" .. i .. "_2.png")
		GUI:Image_loadTexture(ImgMap3, "res/peng_cheng/view_enter_map/map_" .. i .. "_3.png")
		GUI:Image_loadTexture(ImgMap4, "res/peng_cheng/view_enter_map/map_" .. i .. "_4.png")
		GUI:Image_loadTexture(ImgMap5, "res/peng_cheng/view_enter_map/map_" .. i .. "_5.png")
	end
	for i = 1, 3 do
		GUI:addOnClickEvent(type_btn_list[i], function ()
			typeBtnCallback(i)
		end)
	end
	-- ui.selectTypeShow(type_btn_list, ui.type_idx)
	typeBtnCallback(ui.type_idx)

	-- Create ImageViewLeft
	local ImageViewLeft = GUI:Image_Create(UI_MAP_TELEPORT, "ImageViewLeft", 214.5, 309.5,
		"res/peng_cheng/view_enter_map/btn_left.png")
	GUI:addOnClickEvent(ImageViewLeft, function ()
		ui.setBtnScaleEffect(ImageViewLeft)
		ui.select_idx = math.max(1, math.min(5, ui.select_idx - 1))
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		ui.last_pos_x = 0
	end)
	GUI:setAnchorPoint(ImageViewLeft, 0.50, 0.50)
	GUI:setTouchEnabled(ImageViewLeft, true)
	GUI:setTag(ImageViewLeft, -1)
	GUI:setSwallowTouches(ImageViewLeft, true)

	-- Create ImageViewRight
	local ImageViewRight = GUI:Image_Create(UI_MAP_TELEPORT, "ImageViewRight", 920.5, 309.5,
		"res/peng_cheng/view_enter_map/btn_right.png")
	GUI:addOnClickEvent(ImageViewRight, function ()
		ui.setBtnScaleEffect(ImageViewRight)
		ui.select_idx = math.max(1, math.min(5, ui.select_idx + 1))
		ui.setJumpOne(UdtInnerView, {ImgMap1, ImgMap2, ImgMap3, ImgMap4, ImgMap5}, ui.select_idx, 0.6)
		ui.last_pos_x = 0
	end)
	GUI:setAnchorPoint(ImageViewRight, 0.50, 0.50)
	GUI:setTouchEnabled(ImageViewRight, true)
	GUI:setTag(ImageViewRight, -1)
	GUI:setSwallowTouches(ImageViewRight, true)

	-- Create ImageViewEnter
	local ImageViewEnter = GUI:Image_Create(UI_MAP_TELEPORT, "ImageViewEnter", 568.00, 138.00,
		"res/peng_cheng/view_enter_map/btn_enter.png")
	GUI:addOnClickEvent(ImageViewEnter, function ()
		ui.sendEnterMap(ui.type_idx, ui.select_idx)
		ui.setBtnScaleEffect(ImageViewEnter, nil, function ()
			GUI:Win_Close(parent)
		end)
	end)
	GUI:setAnchorPoint(ImageViewEnter, 0.50, 0.50)
	GUI:setTouchEnabled(ImageViewEnter, true)
	GUI:setTag(ImageViewEnter, -1)
	GUI:setSwallowTouches(ImageViewEnter, true)

	-- Create ButtonClose
	local ButtonClose = GUI:Button_Create(UI_MAP_TELEPORT, "ButtonClose", 1011.00, 518.00,
		"res/public/1900000510.png")
	GUI:addOnClickEvent(ButtonClose, function ()
		GUI:Win_Close(parent)
	end)
	GUI:Button_loadTextureNormal(ButtonClose, "res/public/1900000510.png")
	GUI:Button_loadTexturePressed(ButtonClose, "res/public/1900000511.png")
	GUI:setTouchEnabled(ButtonClose, true)
	GUI:setSwallowTouches(ButtonClose, true)
	GUI:setTag(ButtonClose, -1)
end

function ui.udtScrollViewTouchEvent(sender, type, innerView, items)
	-- sender: Enter the control itself
	-- type: Touch type int 0 - 3
	if type == SLDefine.TouchEventType.began then           -- 0 Touch to start
		if ui.block_once then
			ui.block_once = false
		end
		local beginPos = GUI:getTouchBeganPosition(sender)
		ui.last_pos_x = beginPos.x
		-- SL:Print("ScrollEvent beginPos.x:", beginPos.x, " type:", type)
	elseif type == SLDefine.TouchEventType.moved then       -- 1 Touch and move
		local movePos = GUI:getTouchMovePosition(sender)
		-- SL:Print("ScrollEvent movePos.x:", movePos.x, " type:", type)
		ui.touchMoveMapItem(movePos.x, sender, innerView, items, type)

	elseif type == SLDefine.TouchEventType.ended or type == SLDefine.TouchEventType.canceled then       -- 2 Touch ended 3 Touch canceled
		local endPos = GUI:getTouchEndPosition(sender)
		-- SL:Print("ScrollEvent endPos.x:", endPos.x, " type:", type)
		ui.touchMoveMapItem(endPos.x, sender, innerView, items, type)
		ui.last_pos_x = 0
		if ui.block_once then
			ui.block_once = false
		end
	end
end


function ui.udtMoveInnerScrollView(obj, duratin, posX)
	GUI:stopAllActions(obj)
	GUI:runAction(obj, GUI:ActionMoveTo(duratin, posX, 124))
end

function ui.touchMoveMapItem(newX, udtScrollView, innerView, ImgMapBtns, touchType)
	if 0 == ui.last_pos_x then
		ui.last_pos_x = newX
	end
	if ui.last_pos_x == newX then
		return false
	end
	-- SL:Print("touchMoveMapItem newX:", newX, " last_pos_x:", ui.last_pos_x, " type:", touchType)
	-- ui.split_pos_list = {-104.1612, -324.8817, -534.105, -743.457}
	if 0 < ui.last_pos_x - newX then
		-- Ascending (from right to left)
		local curPos = GUI:getPosition(innerView)
		local pos_x = ui.last_pos_x - newX
		if curPos.x - pos_x < innerView.left_limit_x then
			pos_x = curPos.x - innerView.left_limit_x
		end
		local dstX = curPos.x - pos_x
		local dst_idx = 5
		for i = 1, #ui.split_pos_list do
			if ui.split_pos_list[i] < dstX then
				dst_idx = i
				break
			end
		end
		-- SL:Print("ScrollEvent 1 dstX:", dstX, " dst_idx:", dst_idx, " ui.select_idx:", ui.select_idx, " touchType:", touchType)
		-- ui.udtMoveInnerScrollView(innerView, duratin, curPos.x - pos_x)
		GUI:setPosition(innerView, dstX, 124)
		if dst_idx ~= ui.select_idx then
			ui.select_idx = dst_idx
			ui.setJumpOne(innerView, ImgMapBtns, ui.select_idx, 0.6)
			ui.last_pos_x = 0
			GUI:setTouchEnabled(udtScrollView, false)
			GUI:addOnTouchEvent(udtScrollView, function(sender, type) end)
			ui.delayCallback(udtScrollView, 0.6, function(obj)
				GUI:setTouchEnabled(obj, true)
				GUI:addOnTouchEvent(obj, function(sender, type)
					ui.udtScrollViewTouchEvent(sender, type, innerView, ImgMapBtns)
				end)
			end)
			ui.block_once = true
			return true
		end
	else
		-- Descending (from left to right)
		local curPos = GUI:getPosition(innerView)
		local pos_x = newX - ui.last_pos_x
		if curPos.x + pos_x > 0 then
			pos_x = 0 - curPos.x
		end
		local dstX = curPos.x + pos_x
		local dst_idx = 1
		for i = #ui.split_pos_list, 1, -1 do
			if ui.split_pos_list[i] > dstX then
				dst_idx = i + 1
				break
			end
		end
		-- SL:Print("ScrollEvent 2 dstX:", dstX, " dst_idx:", dst_idx, " ui.select_idx:", ui.select_idx, " touchType:", touchType)
		GUI:setPosition(innerView, dstX, 124)
		if dst_idx ~= ui.select_idx then
			ui.select_idx = dst_idx
			ui.setJumpOne(innerView, ImgMapBtns, ui.select_idx, 0.6)
			ui.last_pos_x = 0
			GUI:setTouchEnabled(udtScrollView, false)
			GUI:addOnTouchEvent(udtScrollView, function(sender, type) end)
			ui.delayCallback(udtScrollView, 0.6, function(obj)
				GUI:setTouchEnabled(obj, true)
				GUI:addOnTouchEvent(obj, function(sender, type)
					ui.udtScrollViewTouchEvent(sender, type, innerView, ImgMapBtns)
				end)
			end)
			ui.block_once = true
			return true
		end
	end
	ui.last_pos_x = newX
	return true
end


function ui.setBtnScaleEffect(obj, func1, func2)
	GUI:Button_setBright(obj, true)
	GUI:stopAllActions(obj)
	GUI:runAction(obj,
		GUI:ActionSequence(
			GUI:CallFunc(function()
				if func1 then
					func1()
				end
			end),
			GUI:ActionScaleTo(0.03, 0.9),
			GUI:ActionScaleTo(0.03, 1),
			GUI:CallFunc(function()
				GUI:Button_setBright(obj, false)
				if func2 then
					func2()
				end
			end)
		)
	)
end

function ui.resetItemPosWithAction(ImgMapBtns, selidx, during_time)
	local posX = 2 + 198 + 20
	for i = 1, #ImgMapBtns do
		if i == selidx then
			GUI:stopAllActions(ImgMapBtns[i])
			posX = posX + 130
			GUI:runAction(ImgMapBtns[i], GUI:ActionSpawn(GUI:ActionMoveTo(during_time, posX, 124), GUI:ActionScaleTo(during_time, 1)))
			posX = posX + 130
		else
			GUI:stopAllActions(ImgMapBtns[i])
			posX = posX + 99
			GUI:runAction(ImgMapBtns[i], GUI:ActionSpawn(GUI:ActionMoveTo(during_time, posX, 124), GUI:ActionScaleTo(during_time, 0.7)))
			posX = posX + 99
		end
		posX = posX + 20
	end
end

function ui.delayCallback(obj, during_time, func1)
	GUI:stopAllActions(obj)
	GUI:runAction(obj,
		GUI:ActionSequence(
			GUI:DelayTime(during_time),
			GUI:CallFunc(function()
				if func1 then
					func1(obj)
				end
			end)
		)
	)
end

function ui.resetItemPos(ImgMapBtns, selidx)
	local posX = 2 + 198 + 20
	for i = 1, #ImgMapBtns do
		if i == selidx then
			GUI:setScaleX(ImgMapBtns[i], 1)
			GUI:setScaleY(ImgMapBtns[i], 1)
			posX = posX + 130
			GUI:setPositionX(ImgMapBtns[i], posX)
			posX = posX + 130
		else
			GUI:setScaleX(ImgMapBtns[i], 0.7)
			GUI:setScaleY(ImgMapBtns[i], 0.7)
			posX = posX + 99
			GUI:setPositionX(ImgMapBtns[i], posX)
			posX = posX + 99
		end
		posX = posX + 20
	end
end

function ui.scrollToPercent(ScrollView, selidx, during_time)
	local percent = 1.45
	if 2 == selidx then
		percent = 25.73
	elseif 3 == selidx then
		percent = 50
	elseif 4 == selidx then
		percent = 74.5
	elseif 5 == selidx then
		percent = 98.8
	end
	GUI:ScrollView_scrollToPercentHorizontal(ScrollView, percent, during_time, true)
end

function ui.udtScrollToPercent(innerView, selidx, during_time)
	local new_x = 0
	if 1 == selidx then
		new_x = 0
	elseif 2 == selidx then
		new_x = -231
	elseif 3 == selidx then
		new_x = -449
	elseif 4 == selidx then
		new_x = -667
	else
		new_x = innerView.left_limit_x
	end
	ui.udtMoveInnerScrollView(innerView, during_time, new_x)
end

function ui.setJumpOne(UdtInnerView, ImgMapBtns, selidx, during_time)
	ui.resetItemPosWithAction(ImgMapBtns, selidx, during_time/2)
	-- ui.scrollToPercent(ScrollView, selidx, during_time)
	ui.udtScrollToPercent(UdtInnerView, selidx, during_time)
	SetKeyValue("SelIdx", selidx)
end

function ui.sendEnterMap(type_idx, select_idx)
	SL:SendLuaNetMsg(2100, type_idx, select_idx, 1, "")
end

function ui.selectTypeShow(btns, select_idx)
	for i = 1, #btns do
		if i == select_idx then
			GUI:Button_setBright(btns[i], true)
			-- GUI:setTouchEnabled(btns[i], false)
			GUI:Image_setGrey(btns[i], false)
		else
			GUI:Button_setBright(btns[i], false)
			-- GUI:setTouchEnabled(btns[i], true)
			GUI:Image_setGrey(btns[i], true)
		end
	end
end

function ui.setTypeBtnScaleEffect(obj, func1, func2)
	GUI:Button_setBright(obj, true)
	GUI:stopAllActions(obj)
	GUI:runAction(obj,
		GUI:ActionSequence(
			GUI:CallFunc(function()
				if func1 then
					func1(obj)
				end
			end),
			GUI:ActionScaleTo(0.03, 0.9),
			GUI:ActionScaleTo(0.03, 1),
			GUI:CallFunc(function()
				-- GUI:Button_setBright(obj, false)
				if func2 then
					func2(obj)
				end
			end)
		)
	)
end

return ui